/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.crm.cs.web.SearchAction
   Module Description   :

   Date Created      : 2011/2/1
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.crm.cs.web;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.JspUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.common.web.JsonAction;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.search.Doc;
import com.yaodian100.ec.search.SearchManager;
import com.yaodian100.ec.search.SearchResult;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.service.ShelfService;

/**
 * @author cut7man
 * 
 */
public class SearchAction {
	@Resource
	private SearchManager searchManager;
	@Resource
	private BaseManager baseMgr;
	@Resource
	private ShelfService shelfService;
	private String categoryL1;
	private String page;
	private String productId;
	private String rows;
	private String keyword;
	private Map jsonOut;
	private final Logger logger = LoggerFactory.getLogger(JsonAction.class);

	public String productList() {
		List<Object> objList = new ArrayList<Object>();
		String z = categoryL1;
		if (productId != null && productId.length() > 0) {
			keyword = productId;
		}
		String searchKeywordU = JspUtil.urlDecoding(keyword, "UTF-8");
		String level = "-1";
		String categoryPath = null;
		String applySource = null;
		String searchWithBrief = "1";
		String searchWithDescription = "1";
		String flag2 = null;
		String flag1 = null;
		String flag3 = null;
		String flag4 = null;
		String flag5 = null;
		String flag6 = null;
		String flag7 = null;
		String minPrice = null;
		String maxPrice = null;
		String startDate = null;
		String endDate = null;
		String partner = null;
		String brandId = null;
		if (page == null || page.length() == 0) {
			page = "0";
		} else {
			page = String.valueOf(Integer.valueOf(page) - 1);
		}
		if (categoryL1 != null && categoryL1.length() > 0) {
			Category category = (Category) baseMgr.get(Category.class, Long.valueOf(categoryL1));
			level = category.getLevel();
			categoryPath = category.getCategoryPath();
		}
		String serverName = ServerValue.getFrontHostname();
		String querytype = "";
		String orderby = "";
		String queryoption = "";
		String countquery = "";
		String displayfields = "";
		String weightsyntax = "";
		String blocksyntax = searchManager.blockSyntax_V2(z, searchKeywordU, serverName, applySource, searchWithBrief,
				searchWithDescription, level, categoryPath, flag2, flag3, flag4, flag5, flag6, flag7, minPrice, maxPrice,
				startDate, endDate, flag1, partner, brandId);
		SearchResult result1 = null;
		try {
			result1 = searchManager.query(Integer.valueOf(level), z, searchKeywordU, querytype, Integer.parseInt(page),
					Integer.parseInt(rows), blocksyntax, orderby, queryoption, countquery, displayfields, weightsyntax, false);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer total = 0;
		Integer records = 0;
		if (result1 != null && result1.getDocs() != null) {
			total = result1.getTotal();
			records = result1.getDocs().length;
			for (Doc doc : result1.getDocs()) {
				Product product = (Product) baseMgr.get(Product.class, doc.getProductId());
				Shelf shelf = null;
				if (product != null) {
					shelf = shelfService.getActiveShelfByProductId(product.getProductId());
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("product", product);
				map.put("shelf", shelf);
				objList.add(map);
			}
		}
		jsonOut = new HashMap();
		jsonOut.put("total", total);
		jsonOut.put("page", page);
		jsonOut.put("records", records);
		jsonOut.put("objList", objList);
		return "jsonListOut";
	}

	/**
	 * @return the searchManager
	 */
	public SearchManager getSearchManager() {
		return searchManager;
	}

	/**
	 * @param searchManager the searchManager to set
	 */
	public void setSearchManager(SearchManager searchManager) {
		this.searchManager = searchManager;
	}

	/**
	 * @return the baseMgr
	 */
	public BaseManager getBaseMgr() {
		return baseMgr;
	}

	/**
	 * @param baseMgr the baseMgr to set
	 */
	public void setBaseMgr(BaseManager baseMgr) {
		this.baseMgr = baseMgr;
	}

	/**
	 * @return the shelfService
	 */
	public ShelfService getShelfService() {
		return shelfService;
	}

	/**
	 * @param shelfService the shelfService to set
	 */
	public void setShelfService(ShelfService shelfService) {
		this.shelfService = shelfService;
	}

	/**
	 * @return the categoryL1
	 */
	public String getCategoryL1() {
		return categoryL1;
	}

	/**
	 * @param categoryL1 the categoryL1 to set
	 */
	public void setCategoryL1(String categoryL1) {
		this.categoryL1 = categoryL1;
	}

	/**
	 * @return the page
	 */
	public String getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(String page) {
		this.page = page;
	}

	/**
	 * @return the productId
	 */
	public String getProductId() {
		return productId;
	}

	/**
	 * @param productId the productId to set
	 */
	public void setProductId(String productId) {
		this.productId = productId;
	}

	/**
	 * @return the rows
	 */
	public String getRows() {
		return rows;
	}

	/**
	 * @param rows the rows to set
	 */
	public void setRows(String rows) {
		this.rows = rows;
	}

	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}

	/**
	 * @param keyword the keyword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	/**
	 * @return the jsonOut
	 */
	public Map getJsonOut() {
		return jsonOut;
	}

	/**
	 * @param jsonOut the jsonOut to set
	 */
	public void setJsonOut(Map jsonOut) {
		this.jsonOut = jsonOut;
	}

	/**
	 * @return the logger
	 */
	public Logger getLogger() {
		return logger;
	}
}
